package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.AdChannelDto;
import com.heima.model.common.dtos.PageRequestDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.pojos.wemedia.WmChannel;
import com.heima.model.common.pojos.wemedia.WmNews;
import com.heima.utils.thread.AdminThreadUtil;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 陈辉
 * @data 2024 9:42
 */
@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Autowired
    private WmChannelService wmChannelService;

    @Autowired
    private WmChannelMapper wmChannelMapper;

    /**
     * 添加频道
     *
     * @param adChannelDto
     */
    @Override
    public ResponseResult saveChannel(AdChannelDto adChannelDto) {
        //1、参数校验
        if (adChannelDto == null || adChannelDto.getName() == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2、实体类赋值
        WmChannel wmChannel = new WmChannel();
        BeanUtils.copyProperties(adChannelDto, wmChannel);
        wmChannel.setIsDefault(true);
        wmChannel.setCreatedTime(new Date());
        wmChannel.setStatus(adChannelDto.getStatus());

        //3、查询数据库,查看词条是否重复
        List<WmChannel> wmChannels = wmChannelService.list();
        if (wmChannels != null && !wmChannels.isEmpty()) {
            for (WmChannel channel : wmChannels) {
                if (channel.getName().equals(adChannelDto.getName())) {
                    return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
                }
            }
        }

        //4、添加频道信息返回
        wmChannelService.save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 分页查询频道列表
     *
     * @param adChannelDto
     * @return
     */
    @Override
    public ResponseResult pageList(AdChannelDto adChannelDto) {
        //1、参数校验
        if (adChannelDto == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页参数检查
        adChannelDto.checkParam();

      /*  //2、获取当前登陆人信息
        String userId = AdminThreadUtil.get();*/

        //3、构建分页查询参数
        Page page = new Page(adChannelDto.getPage(), adChannelDto.getSize());

        //4、构建分页条件
        //4.1、按频道名称模糊查询
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        if (adChannelDto.getName() != null) {
            queryWrapper.like(WmChannel::getName, adChannelDto.getName());
        }

        //4.2、按状态精确查找
        if (adChannelDto.getStatus() != null) {
            queryWrapper.eq(WmChannel::getStatus, adChannelDto.getStatus());
        }
        //4.3、按创建时间倒序查询
        queryWrapper.orderByDesc(WmChannel::getCreatedTime);

        //5、开启查询:
        page(page, queryWrapper);

        //5、封装结果返回  total  list
        PageResponseResult pageResponseResult = new PageResponseResult((int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }

    /**
     * 删除频道
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult delChannel(Integer id) {
        //1、参数校验
        if (id == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2、根据id查询频道信息
        WmChannel wmChannel = wmChannelMapper.selectById(id);

        //3、判断条件
        if (wmChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_STATUS_ERROR);
        }

        //4、删除频道信息
        wmChannelMapper.deleteById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改频道信息
     *
     * @param adChannelDto
     * @return
     */
    @Override
    public ResponseResult updateChannel(AdChannelDto adChannelDto) {
        //1、参数校验
        if (adChannelDto == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2、转化为实体类
        WmChannel wmChannel = new WmChannel();
        BeanUtils.copyProperties(adChannelDto, wmChannel);
        wmChannel.setCreatedTime(new Date());

        //todo:3、判断频道是否被引用

        //4、修改频道信息存入数据库
        wmChannelMapper.updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
